package com.ym.controller.management;

import com.github.pagehelper.PageInfo;

import com.ym.core.domain.APIResponse;
import com.ym.core.domain.BaseController;
import com.ym.core.exception.BusinessException;
import com.ym.core.utils.excel.ExportExcelUtil;
import com.ym.core.utils.excel.ImportExcelUtil;

import com.ym.medical.domain.entity.HospitalDeptEntity;
import com.ym.medical.domain.entity.HospitalEntity;
import com.ym.medical.domain.entity.StandardDeptEntity;
import com.ym.medical.domain.param.HospitalDeptQryParam;
import com.ym.medical.domain.vo.HospitalDeptVo;
import com.ym.medical.domain.vo.HospitalDeptVo2;
import com.ym.medical.enums.HospitalDeptTypeEnum;
import com.ym.medical.service.IHospitalDeptSV;
import com.ym.medical.service.IHospitalSV;
import com.ym.medical.service.IStandardDeptSV;
import com.ym.medical.service.ISysDictSV;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 综合管理中心
 */
@Api(tags = "科室管理")
@Controller
@RequestMapping("/management/hosDept")
public class HospitalDeptController extends BaseController {

	private static Logger logger = LoggerFactory.getLogger(HospitalDeptController.class);

	@Autowired
	private IHospitalDeptSV iHospitalDeptSV;

	@Autowired
	private IHospitalSV iHospitalSV;

	@Autowired
	private IStandardDeptSV iStandardDeptSV;
	@Autowired
	private ISysDictSV iSysDictSV;


	@ApiOperation("科室列表查询检索")
	@GetMapping("/getHosDeptList")
	@ResponseBody
	public APIResponse<PageInfo<HospitalDeptVo>> getHospitalDeptList(
			@ApiParam(name = "pageNo", value = "页码", required = true)
			@RequestParam(name = "pageNo", required = true, defaultValue = "1")
					int pageNo,
			@ApiParam(name = "limit", value = "每页数量", required = false)
			@RequestParam(name = "limit", required = false, defaultValue = "10")
					int limit,
			@ApiParam(name = "hospitalDeptModel", value = "科室信息", required = false)
					HospitalDeptQryParam hospitalDeptModel) throws Exception {
		PageInfo<HospitalDeptVo> hospitalDeptPageInfo =
				iHospitalDeptSV.getHospitalDeptList(pageNo, limit, hospitalDeptModel);
		return successData(hospitalDeptPageInfo);
	}

	@ApiOperation("科室导入")
	@PostMapping("/importHosDept")
	@ResponseBody
	public APIResponse<Map<String, Object>> importHosDept(
			@ApiParam(name = "excelFile", value = "科室EXCEL信息", required = true)
			@RequestBody MultipartFile excelFile) throws Exception {
		if (excelFile.isEmpty()) {
			return failMsg("文件不存在");
		}
		Map<String, Object> map = new HashMap<>();
		InputStream inputStream = null;
		int startLine = 2;
		int rows = 0;
		try {
			String fileName = excelFile.getOriginalFilename();
			inputStream = excelFile.getInputStream();
			List<HospitalDeptEntity> hospitalDeptList = ImportExcelUtil.readExcel(
					inputStream, fileName, startLine, HospitalDeptEntity.class, ImportExcelUtil.getHosDeptTemplate());
			if (hospitalDeptList.isEmpty()) {
				throw new BusinessException("您的excel没有有效的数据 请添加数据后再试!");
			}
			map = iHospitalDeptSV.importHosDept(hospitalDeptList);
		} catch (Exception e) {
			if (e instanceof BusinessException) {
				throw new BusinessException(e.getMessage());
			}
			logger.info("HospitalDeptController importHosDept", e.getMessage());
		} finally {
			if (inputStream != null) {
				inputStream.close();
			}
		}

		return successData(map);
	}

	@ApiOperation("科室导出")
	@GetMapping("/exportHosDept")
	public void exportHosDept(
			@ApiParam(name = "pageNo", value = "页码", required = true)
			@RequestParam(name = "pageNo", required = true, defaultValue = "1")
					int pageNo,
			@ApiParam(name = "limit", value = "每页数量", required = false)
			@RequestParam(name = "limit", required = false, defaultValue = "1000000")
					int limit,
			@ApiParam(name = "hospitalDeptModel", value = "科室信息", required = false)
					HospitalDeptQryParam hospitalDeptModel,
			HttpServletResponse response) throws Exception {
		ExportExcelUtil exportExcelUtil = new ExportExcelUtil();
		String titleName = "科室信息表";
		String[] headerString = {"医院名称", "科室编码", "科室名称", "标准科室编码", "标准科室名", "科室类型"};
		String[] headerField = {"hosName", "deptCode", "deptName", "deptCode", "deptName", "deptTypeName"};
		PageInfo<HospitalDeptVo2> hospitalDeptPageInfo = iHospitalDeptSV.getAllHospitalDept(pageNo, limit, hospitalDeptModel);
		exportExcelUtil.exportExcelForTotal
				(titleName, headerString, headerField, null, hospitalDeptPageInfo.getList(), response, null);

	}


	@ApiOperation("科室删除")
	@PostMapping("/deleteHosDept")
	@ResponseBody
	public APIResponse deleteHosDept(
			@ApiParam(name = "deptIds", value = "科室ids数组", required = true) @RequestBody Long[] deptId) throws Exception {
		int rows = iHospitalDeptSV.deleteByIds(deptId);
		if (rows == 0) {
			return failMsg("删除失败");
		}

		return successMsg("删除成功");
	}

	@ApiOperation("科室修改")
	@PostMapping("/updateHosDept")
	@ResponseBody
	public APIResponse updateHosDept(
			@ApiParam(name = "hospitalDept", value = "科室信息", required = true)
			@RequestBody HospitalDeptEntity hospitalDept) throws Exception {

		int rows = iHospitalDeptSV.updateHosDept(hospitalDept);
		if (rows != 1) {
			return failMsg("更改失败");
		}

		return successMsg("更改成功");
	}

	@ApiOperation("科室新增")
	@PostMapping("/addHosDept")
	@ResponseBody
	public APIResponse addHosDept(
			@ApiParam(name = "hospitalDept", value = "科室信息", required = true)
			@RequestBody HospitalDeptEntity hospitalDept) throws Exception {
		int rows = iHospitalDeptSV.addHosDept(hospitalDept);
		if (rows != 1) {
			return failMsg("新增失败");
		}
		return successMsg("新增成功");
	}

	@ApiOperation("标准科室名和医院名map")
	@GetMapping("/mapOfHosNameSdeptName")
	@ResponseBody
	public APIResponse<Map<String, Object>> mapOfHosNameSdeptName() {
		List<HospitalEntity> hospitals = iHospitalSV.selectHosName();
		List<StandardDeptEntity> standardDepts = iStandardDeptSV.selectSDeptName();
		Map<String, Object> map = new HashMap<>();
		map.put("hospitals", hospitals);
		map.put("standardDepts", standardDepts);
		return successData(map);
	}


	@ApiOperation("科室类型")
	@GetMapping("/selectDeptType")
	@ResponseBody
	public APIResponse<Map<String, String>> selectDeptType() {
		Map<String, String> dictValues = iSysDictSV.getDictValuesToMap(HospitalDeptTypeEnum.class);
		if (null == dictValues) {
			return failMsg("请求失败");
		}
		return successData(dictValues);
	}


}
